package com.example.zhaojianxu.signin.common.account.view

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bank.main.BankMainActivity
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.car.main.CarMainActivity
import com.example.zhaojianxu.signin.common.account.viewModel.AccountViewModel
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.util.ToastUtil
import com.example.zhaojianxu.signin.englishStudy.main.EnglishMainActivity
import com.example.zhaojianxu.signin.englishgame.main.EnglishGameActivity
import com.example.zhaojianxu.signin.psychological.main.PsycMainActivity
import com.example.zhaojianxu.signin.securities.main.SecuritiesMainActivity
import com.example.zhaojianxu.signin.signIn.main.SignMainActivity
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.common_title_bar.*

/**
 * 登录
 */
class LoginActivity : BaseActivity(), View.OnClickListener {

    private lateinit var mAccountViewModel: AccountViewModel

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.login_login -> {
                val account = login_account.text.toString()
                val password = login_password.text.toString()

                if (!account.isEmpty() && !password.isEmpty()) {
                    mAccountViewModel.postLogin(account, password)
                } else {
                    ToastUtil.instance.showToast(this, "请填写完全！")
                }
            }
            R.id.login_logon -> {
                toOtherActivity(LogonActivity::class.java)
            }
        }
    }

    override fun initView() {
        common_back.visibility = View.GONE
        common_setting.visibility = View.GONE
        common_title.text = "登录"
        login_login.setOnClickListener(this)
        login_logon.setOnClickListener(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

    }

    override fun initData() {
        mAccountViewModel = ViewModelProviders.of(this)[AccountViewModel::class.java]

        mAccountViewModel.mLogin.observe(this, Observer {

            if (it != null && it.uid != -1 && it.name?.isNotEmpty() == true) {
                ComConstance.UID = it?.uid!!
                selectOtherModule(ComConstance.COM_VERSION)

                finish()
            }
        })
    }

    private fun selectOtherModule(type: String) {
        when (type) {
            ComConstance.TYPE_BANK -> {
                toOtherActivity(BankMainActivity::class.java)
            }
            ComConstance.TYPE_SIGN -> {
                toOtherActivity(SignMainActivity::class.java)
            }
            ComConstance.TYPE_ENGLISH_GAME -> {
                toOtherActivity(EnglishGameActivity::class.java)
            }
            ComConstance.TYPE_ENGLISH_STUDY -> {
                toOtherActivity(EnglishMainActivity::class.java)
            }
            ComConstance.TYPE_PSYCHOLOGICAL -> {
                toOtherActivity(PsycMainActivity::class.java)
            }
            ComConstance.TYPE_SECURITIES -> {
                toOtherActivity(SecuritiesMainActivity::class.java)
            }
            ComConstance.TYPE_CAR -> {
                toOtherActivity(CarMainActivity::class.java)
            }
        }
    }


}
